﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace tsgl
{
    public partial class bibliothecaInfoEdit : Form
    {
        public bibliothecaInfoEdit(string str)
        {
            InitializeComponent();
            this.txtBibliothecaId.Text = str;
        }

        public string editBibliothecaId;
        private void bibliothecaInfoEdit_Load(object sender, EventArgs e)
        {
            editBibliothecaId = this.txtBibliothecaId.Text;
            DataTable dt;
            cboBookType.DataSource = bibliothecaClass.getBookTypeNameAll();
            cboBookType.DisplayMember = "图书类型名称";
            cboPlace.DataSource = bibliothecaClass.getDepositary();
            cboPlace.DisplayMember = "馆藏地点编号";
            dt = bibliothecaClass.getBibliothecaData(editBibliothecaId);
            txtBibliothecaId.Text = dt.Rows[0]["书目编号"].ToString();
            txtBookName.Text = dt.Rows[0]["图书名称"].ToString();
            txtAuthor.Text = dt.Rows[0]["作者"].ToString();
            txtPublisher.Text = dt.Rows[0]["出版社名称"].ToString();
            txtBookISBN.Text = dt.Rows[0]["ISBN"].ToString();
            nudPublishDate.Text = dt.Rows[0]["出版日期"].ToString();
            txtBookPages.Text = dt.Rows[0]["图书页数"].ToString();
            txtPrice.Text = dt.Rows[0]["价格"].ToString();
            cboBookType.Text = dt.Rows[0]["图书类型名称"].ToString();
            try
            {
                nudTotalNumber.Value = int.Parse(dt.Rows[0]["总藏书数量"].ToString());
            }
            catch
            {
                nudTotalNumber.Value = 0;
            }
            try
            {
                nudNowNumber.Value = int.Parse(dt.Rows[0]["现存数量"].ToString());
            }
            catch
            {
                nudNowNumber.Value = 0;
            }
            cboPlace.Text = dt.Rows[0]["馆藏地点"].ToString();
            txtSynopsis.Text = dt.Rows[0]["简介"].ToString();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (txtBibliothecaId.Text.Trim() == "")
            {
                MessageBox.Show("书目编号不能为空！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtBibliothecaId.Focus();
            }
            else if (txtBookName.Text.Trim() == "")
            {
                MessageBox.Show("图书名称不能为空！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtBookName.Focus();
            }


            else if (cboBookType.Text.Trim() == "请选择")
            {
                MessageBox.Show("请选择图书类型！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                cboBookType.Focus();
            }

            else if (txtAuthor.Text.Trim() == "")
            {
                MessageBox.Show("作者不能为空！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtAuthor.Focus();
            }

            else if (txtPublisher.Text.Trim() == "")
            {
                MessageBox.Show("出版社不能为空！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtPublisher.Focus();
            }

            else if (txtBookPages.Text.Trim() == "")
            {
                MessageBox.Show("图书页数不能为空！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtBookPages.Focus();
            }

            else if (txtPrice.Text.Trim() == "")
            {
                MessageBox.Show("价格不能为空！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtPrice.Focus();
            }

            else if (txtBookISBN.Text.Trim() == "")
            {
                MessageBox.Show("图书的ISBN不能为空!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtBookISBN.Focus();
            }

            else if (cboPlace.Text.Trim() == "请选择")
            {
                MessageBox.Show("藏书地点不能为空！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                cboPlace.Focus();
            }
            else
            {
                DataTable dt = bibliothecaClass.getPublisherISBN(txtPublisher.Text);
                if (dt.Rows.Count != 0)
                {
                    publisherISBN = dt.Rows[0][0].ToString();
                }
                if (txtBibliothecaId.Text != editBibliothecaId)
                {
                    dt.Clear();
                    dt = bibliothecaClass.getBibliothecaData(txtBibliothecaId.Text);
                    if (dt.Rows.Count == 0)
                    {
                        Boolean updateResult;
                        updateResult = bibliothecaClass.bibliothecaEditAll(txtBibliothecaId.Text.Trim(), txtBookName.Text.Trim(), txtAuthor.Text.Trim(), publisherISBN, txtBookISBN.Text.Trim(), nudPublishDate.Text.Trim(), bookPages, bookPrice, bookType, Convert.ToInt32(nudTotalNumber.Value), Convert.ToInt32(nudNowNumber.Value), cboPlace.Text.Trim(), txtSynopsis.Text, editBibliothecaId);
                        if (updateResult == true)
                        {
                            MessageBox.Show("书目信息修改成功！", "提示信息");
                            this.Close();
                        }
                        else
                        {
                            MessageBox.Show("书目信息修改失败，请重试！", "提示信息");
                            this.txtBibliothecaId.Focus();
                        }
                    }
                    else
                    {
                        MessageBox.Show("此书目编号已经存在，请重新输入");
                        txtBibliothecaId.Text="";
                        txtBibliothecaId.Focus();
                    }
                }
                else
                {
                    Boolean updateResult;
                    updateResult = bibliothecaClass.bibliothecaEditPart(txtBibliothecaId.Text.Trim(), txtBookName.Text.Trim(), txtAuthor.Text.Trim(), publisherISBN, txtBookISBN.Text.Trim(), nudPublishDate.Text.Trim(), bookPages, bookPrice, bookType, Convert.ToInt32(nudTotalNumber.Value), Convert.ToInt32(nudNowNumber.Value), cboPlace.Text.Trim(), txtSynopsis.Text);
                    if (updateResult == true)
                    {
                       MessageBox.Show("书目信息修改成功！", "提示信息");
                       this.Close();
                    }
                    else
                    {
                       MessageBox.Show("书目信息修改失败，请重试！", "提示信息");
                       this.txtBibliothecaId.Focus();
                    }
                 }
             }
        }

        public string bookType;
        private void cboBookType_SelectedValueChanged(object sender, EventArgs e)
        {
            DataTable dt = bibliothecaClass.getBookTypeId(cboBookType.Text);
            if (dt.Rows.Count != 0)
            {
                bookType = dt.Rows[0][0].ToString();
            }
        }

        public string publisherISBN;
        private void btnSelectPublisher_Click(object sender, EventArgs e)
        {
            selectPublisher frmSelect = new selectPublisher();
            frmSelect.StartPosition = FormStartPosition.Manual;
            frmSelect.Left = this.txtPublisher.Left + this.Left;
            frmSelect.Top = this.txtPublisher.Top + this.Top + this.txtPublisher.Height + this.Height - this.ClientRectangle.Height;
            frmSelect.ShowDialog();
            this.txtPublisher.Text = frmSelect.getTreeViewSelectNode();
            DataTable dt = bibliothecaClass.getPublisherISBN(this.txtPublisher.Text);
            if (dt.Rows.Count != 0)
            {
                publisherISBN = dt.Rows[0][0].ToString();
            }
        }

        public int bookPages;
        private void txtBookPages_TextChanged(object sender, EventArgs e)
        {
            if (txtBookPages.Text.Trim() != "")
            {
                try
                {
                    bookPages = int.Parse(this.txtBookPages.Text.Trim());
                }
                catch (Exception)
                {
                    MessageBox.Show("图书页数只能为数字型数据", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    txtBookPages.Text = "";
                    txtBookPages.Focus();
                }
            }
        }

        public float bookPrice;
        private void txtPrice_TextChanged(object sender, EventArgs e)
        {
            if (this.txtPrice.Text.Trim() != "")
            {
                try
                {
                    bookPrice = float.Parse(this.txtPrice.Text.Trim());
                }
                catch (Exception)
                {
                    MessageBox.Show("价格只能为数字型数据", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    txtPrice.Text = "";
                    txtPrice.Focus();
                }
            }
        }

        private void btnCancle_Click(object sender, EventArgs e)
        {
            this.txtBibliothecaId.Text = "";
            this.txtBookName.Text = "";
            this.txtAuthor.Text = "";
            this.txtPublisher.Text = "";
            this.txtBookISBN.Text = "";
            this.txtBookPages.Text = "";
            this.txtPrice.Text = "";
            this.cboBookType.SelectedIndex = -1;
            this.cboBookType.Text = "请选择";
            this.nudTotalNumber.Value = 0;
            this.nudNowNumber.Value = 0;
            this.cboPlace.SelectedIndex = -1;
            this.cboPlace.Text = "请选择";
            this.txtSynopsis.Text = "";
        }
    }
}